//add by yuanfei
#include "CCLuaEngine.h"
#include "tolua++.h"
#include "tolua_fix.h"
#include "CCControlButton.h"
#include "CCScrollView.h"
#include "LuaCCBReader.h"
#include "SimpleAudioEngine.h"

ButtonHandler* ButtonHandler::pSharedButtonHandler = NULL;

ButtonHandler* ButtonHandler::sharedButtonHandler()
{
    if (! pSharedButtonHandler)
    {
        pSharedButtonHandler= new ButtonHandler();
    }

    return pSharedButtonHandler;
}

ButtonHandler::ButtonHandler(){
}
void ButtonHandler::handle(CCObject* pSender, CCControlEvent event){
  //CCLog("handle %p %p %d", pSender, this, names_.size());
  //CCLog("ButtonHandler::handle %s %p %d", names_[pSender].c_str(), pSender, event);
  CCControlButton *b = (CCControlButton*)pSender;

  //只处理button up事件
  if(b->isTouchEndOnly() && (event!=CCControlEventTouchUpInside || b->isTouchMoved())){
    return;
  }
  //只要有移动，就不处理CCControlEventTouchUpInside事件 
  if(b->isTouchMoved() && event==CCControlEventTouchUpInside){
    return;
  }
  
  if(event==CCControlEventTouchDragExit || event==CCControlEventTouchUpInside){
    //CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("button_click.mp3");
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    pEngine->executeHandleFunction(names_[pSender].c_str(), pSender, event);
  }
}
void ButtonHandler::menuHandle(CCObject* obj){
  //CCLog("ButtonHandler::menuHandle %s %p", names_[obj].c_str(), obj);
  //CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("button_click.mp3");
  CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
  pEngine->executeHandleFunction(names_[obj].c_str(), obj, CCControlEventTouchDown);
}

void ButtonHandler::addName(const char* name, CCObject* sender){
  //CCLog("addName %d", names_.size());
  names_[(void*)sender] = name;
}


MySelectorResolver::~MySelectorResolver(){};
SEL_CCControlHandler MySelectorResolver::onResolveCCBCCControlSelector (CCObject *pTarget, CCString *pSelectorName){
  //CCLog("MySelectorResolver %p", pTarget);
  //CCLog("MySelectorResolver %s", pSelectorName->getCString());
  ButtonHandler::sharedButtonHandler()->addName(pSelectorName->getCString(), pTarget);
  return (SEL_CCControlHandler)&ButtonHandler::handle;
}
SEL_MenuHandler MySelectorResolver::onResolveCCBCCMenuItemSelector (CCObject *pTarget, CCString *pSelectorName){
  //CCLog("MySelectorResolver %p", pTarget);
  //CCLog("MySelectorResolver %s", pSelectorName->getCString());
  ButtonHandler::sharedButtonHandler()->addName(pSelectorName->getCString(), pTarget);
  return (SEL_MenuHandler)&ButtonHandler::menuHandle;
}



int tolua_Cocos2d_CCBReader_load(lua_State* tolua_S)
{
  timeval beg,end;
  gettimeofday(&beg, NULL);
  const char* pszFileName = ((const char*)  tolua_tostring(tolua_S,2,0));
  {
    MySelectorResolver mysr;
    //CCLog("tolua_Cocos2d_CCBReader_load --> %s", pszFileName);
    CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
    CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary, NULL, &mysr, NULL);
    ccbReader->autorelease();

    //ButtonHandler *handler = new ButtonHandler();
    CCNode *node = ccbReader->readNodeGraphFromFile(pszFileName, ButtonHandler::sharedButtonHandler());
    //CCLog("load %p", node);
    //CCLog("load handler %p", ButtonHandler::sharedButtonHandler());

    CCNode* tolua_ret = node;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCLayer");
  }
  gettimeofday(&end, NULL);
  float cost = (end.tv_sec-beg.tv_sec)*1000 + (end.tv_usec-beg.tv_usec)/1000.0;
  //printf("tolua_Cocos2d_CCBReader_load cost %f\n", cost);
  return 1;
}

int tolua_Cocos2d_CCButton_setEnabled(lua_State* tolua_S){
  CCControl* self = (CCControl*)  tolua_tousertype(tolua_S,1,0);
  bool enabled = ((bool)  tolua_toboolean(tolua_S,2,0));
  self->setEnabled(enabled);
  return 1;
}
int tolua_Cocos2d_CCButton_setTouchEndOnly(lua_State* tolua_S){
  CCControlButton* self = (CCControlButton*)  tolua_tousertype(tolua_S,1,0);
  bool touchEndOnly = ((bool)  tolua_toboolean(tolua_S,2,0));
  self->setTouchEndOnly(touchEndOnly);
  return 1;
}
int tolua_Cocos2d_CCButton_setTitle(lua_State* tolua_S){
  CCControlButton* self = (CCControlButton*)  tolua_tousertype(tolua_S,1,0);
  const char* title = ((const char*)  tolua_tostring(tolua_S,2,0));
  //CCLabelTTF *lb = (CCLabelTTF*)self->getTitleLabelForState(CCControlStateNormal);
  //lb->setString(title);
  CCLOG("setTitle %s", title);
  self->setTitleForState(CCString::create(title), CCControlStateNormal);
  //self->setTitleLabelForState(CCLabelTTF::create(title, "Helvetica", 28), CCControlStateNormal);
  //self->setTouchEndOnly(touchEndOnly);
  return 1;
}

int tolua_Cocos2d_CCButton_setBackgroundImg(lua_State* tolua_S){
  CCControlButton* self = (CCControlButton*)  tolua_tousertype(tolua_S,1,0);
  const char* normal = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* highlight = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* disabled = ((const char*)  tolua_tostring(tolua_S,4,0));
  CCScale9Sprite* nsp = CCScale9Sprite::create(normal);
  CCScale9Sprite* hsp = CCScale9Sprite::create(highlight);
  CCScale9Sprite* dsp = CCScale9Sprite::create(disabled);
  //CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(nsp->getTexture(),nsp->getTextureRect()); 
  //self->setBackgroundSprite(nsp);
  self->setBackgroundSpriteForState(nsp, CCControlStateNormal);
  self->setBackgroundSpriteForState(hsp, CCControlStateHighlighted);
  self->setBackgroundSpriteForState(dsp, CCControlStateDisabled);
  return 1;
}
int tolua_Cocos2d_CCButton_createWithFrameName(lua_State* tolua_S){
  const char* callback = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* normal = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* highlight = ((const char*)  tolua_tostring(tolua_S,4,0));
  const char* disabled = ((const char*)  tolua_tostring(tolua_S,5,0));
  {
    CCControlButton * button = CCControlButton::create();
    CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(normal);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 
    
    frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(highlight);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 

    frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disabled);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal);

    ((CCControl *)button)->addTargetWithActionForControlEvents(
      ButtonHandler::sharedButtonHandler(), (SEL_CCControlHandler)&ButtonHandler::handle, CCControlEventTouchUpInside);
    ButtonHandler::sharedButtonHandler()->addName(callback, button);    
    
    button->setPreferredSize(frame->getOriginalSize());
    
    CCLog("tolua_Cocos2d_CCButton_create %s %s", callback, normal); 
    CCControlButton* tolua_ret = button;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCButton");
  }
  return 1;
}

int tolua_Cocos2d_CCButton_createWithFileName(lua_State* tolua_S){
  const char* callback = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* normal = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* highlight = ((const char*)  tolua_tostring(tolua_S,4,0));
  const char* disabled = ((const char*)  tolua_tostring(tolua_S,5,0)); 
  const char* title = ((const char*)  tolua_tostring(tolua_S,6,0));
  string titleStr;
  if(title){
    titleStr = title;
  }
  {
    CCControlButton * button = CCControlButton::create(title, "Helvetica", 28);
    button->setZoomOnTouchDown(false);
    CCSpriteFrame *frame = NULL;
    CCSprite * nsp = CCSprite::create(normal);
    CCSprite * hsp = CCSprite::create(highlight);
    CCSprite * dsp = CCSprite::create(disabled);
    CCRect rect = nsp->getTextureRect();
    //printf("button %f %f %f %f\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    {
      frame = CCSpriteFrame::createWithTexture(dsp->getTexture(),dsp->getTextureRect()); 
      button->setBackgroundSpriteFrameForState(frame, CCControlStateDisabled);
    }
    {
      frame = CCSpriteFrame::createWithTexture(nsp->getTexture(),nsp->getTextureRect()); 
      button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 
    }
    { 
      frame = CCSpriteFrame::createWithTexture(hsp->getTexture(),hsp->getTextureRect()); 
      button->setBackgroundSpriteFrameForState(frame, CCControlStateHighlighted); 
    }
      
    ((CCControl *)button)->addTargetWithActionForControlEvents(
      ButtonHandler::sharedButtonHandler(), (SEL_CCControlHandler)&ButtonHandler::handle, 
      CCControlEventTouchUpInside|CCControlEventTouchDragOutside|CCControlEventTouchDragEnter|CCControlEventTouchDragInside
      |CCControlEventTouchDragExit);
      //CCControlEventTouchUpInside);
    ButtonHandler::sharedButtonHandler()->addName(callback, button);    
    
    button->setPreferredSize(frame->getOriginalSize());
    
    //CCLog("tolua_Cocos2d_CCButton_create %s %s", callback, normal); 
    CCControlButton* tolua_ret = button;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCButton");
  }
  return 1;
}
int tolua_Cocos2d_CCScrollView_setContainer(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  CCNode* container = ((CCNode*)  tolua_tousertype(tolua_S,2,0));
  {
    self->setContainer(container); 
  }
  return 1;
}

int tolua_Cocos2d_CCScrollView_getContainer(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  {
    CCNode* container = self->getContainer(); 
    CCNode* tolua_ret = container;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCLayer");
  }
  return 1;
}
int tolua_Cocos2d_CCScrollView_setContentOffset(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  CCPoint pos = *((CCPoint*)  tolua_tousertype(tolua_S,2,0));
  self->setContentOffset(pos);
}
int tolua_Cocos2d_CCScrollView_getContentOffset(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  //CCPoint pos = *((CCPoint*)  tolua_tousertype(tolua_S,2,0));
  CCPoint p = self->getContentOffset();
  
  CCPoint tolua_ret = p; 
  void* tolua_obj = Mtolua_new((CCPoint)(tolua_ret));
  tolua_pushusertype(tolua_S,tolua_obj,"CCPoint");
  tolua_register_gc(tolua_S,lua_gettop(tolua_S));
  return 1;
}

int tolua_Cocos2d_CCScrollView_getViewSize(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  {
    CCSize size = self->getViewSize(); 
    CCSize tolua_ret = size; 
#ifdef __cplusplus
    void* tolua_obj = Mtolua_new((CCSize)(tolua_ret));
     tolua_pushusertype(tolua_S,tolua_obj,"CCSize");
    tolua_register_gc(tolua_S,lua_gettop(tolua_S));
#else
    void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(CCSize));
     tolua_pushusertype(tolua_S,tolua_obj,"CCSize");
    tolua_register_gc(tolua_S,lua_gettop(tolua_S));
#endif
  }
  return 1;
}
